package com.example.demo.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.domain.Commodity;
import com.example.demo.domain.Supplier;
import com.example.demo.exception.CommodityException;
import com.example.demo.service.ICommodityService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-10-30
 */
@Controller
@RequestMapping("/commodity")
public class CommodityController {

    @Autowired
    ICommodityService commodityService;


    @ApiOperation("根据Id查询商品")
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('commodity/query')")
    public ResponseEntity<Commodity> getProduct(@ApiParam("商品Id")@PathVariable long id){
        Commodity result = commodityService.getById(id);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }

    @ApiOperation("根据条件查询商品")
    @GetMapping("")
    @PreAuthorize("hasAuthority('commodity/query')")
    public ResponseEntity<IPage<Commodity>> findCommodity(@ApiParam("商品名称")String name,
                                      @ApiParam("商品进价")Double purchase_price,
                                      @ApiParam("商品售价")Double sale_price,
                                      @ApiParam("进货日期")String purchase_date,
                                      @ApiParam("出售日期")String sale_date,
                                      @ApiParam("供应商名称")String supplierName,
                                      @ApiParam("页码")@RequestParam(defaultValue = "0")Integer pageNum,
                                      @ApiParam("每页记录数") @RequestParam(defaultValue = "10")Integer pageSize){
        Map<String,Object> condition=new HashMap<>();
        if(name!=null) {
            condition.put("name","%"+name+"%");
        }
        if(purchase_price!=null) {
            condition.put("purchase_price",purchase_price);
        }
        if(sale_price!=null) {
            condition.put("sale_price",sale_price);
        }
        if(purchase_date!=null) {
            condition.put("purchase_date",purchase_date);
        }
        if(sale_date!=null) {
            condition.put("sale_date",sale_date);
        }
        if(supplierName!=null) {
            condition.put("supplierName",supplierName);
        }
        IPage<Commodity> result =commodityService.findCommodity(condition, pageNum,pageSize);
        return ResponseEntity.ok(result);
    }


    @ApiOperation("添加商品")
    @PostMapping("")
    @PreAuthorize("hasAuthority('commodity/update')")
    public ResponseEntity<Commodity> addCommodity(@RequestBody Commodity commodity) throws CommodityException {
        Commodity result = commodityService.addCommodity(commodity);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("修改商品信息")
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('commodity/update')")
    public ResponseEntity<Void> updateProduct(@PathVariable long id,@RequestBody Commodity product)
            throws CommodityException {
        commodityService.updateCommodity(id,product);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('commodity/update')")
    public ResponseEntity<Void> deleteProduct(@PathVariable long id){
        commodityService.removeById(id);
        return ResponseEntity.ok().build();
    }

}

